import { makeAutoObservable } from 'mobx';
import { PAGE } from '@/enums/common';
import Http from '../utils/http';
async function handleOrder(url: string, payload: any) {
  const result = await Http({
    url,
    body: payload,
  });
  return result;
}
class HomeStore {
  detail = {};
  comments: any[] = [];
  page = PAGE;
  showLoading = true;
  reloadCommentsNum = 0;
  order = null;
  resetData = () => {
    this.detail = {};
    this.comments = [];
    this.page = PAGE;
    this.showLoading = true;
    this.reloadCommentsNum = 0;
  };
  reloadComments() {
    this.reloadCommentsNum++;
    this.page = {
      ...PAGE,
      pageNum: this.page.pageNum + 1,
    };
  }
  constructor() {
    makeAutoObservable(this);
  }

  async getDetailAsync(payload: { id: number }) {
    try {
      const detail: any = await Http({
        url: '/house/detail',
        body: payload,
      });
      console.log(detail);
      
      this.detail = detail;
    } catch (e) {
      console.log(e, '错误');
    }
  }
  async getCommentsAsync(payload: { id: number }) {
    const { comments, page } = this;
    const lists: any = await Http({
      url: '/comments/lists',
      body: {
        ...payload,
        pageSize: page.pageSize,
        pageNum: page.pageNum,
      },
    });

    this.comments = [...comments, ...lists];
    this.showLoading = lists.length ? true : false;
  }

  async addCommentsAsync(payload: any) {
    const result = await Http({
      url: '/comments/add',
      body: payload,
    });
    console.log(result);

    if (result) {
      this.comments = [];
      this.page = PAGE;
      this.showLoading = true;
      this.reloadCommentsNum = 0;
    }
  }
  async hasOrderAsync(payload: any) {
    const result: any = await handleOrder('/orders/hasOrder', payload);

    this.order = result;
  }
  async addOrderAsync(payload: any) {
    const result: any = await handleOrder('/orders/addOrder', payload);
    this.order = result;
  }
  async delOrderAsync(payload: any) {
    const result: any = await handleOrder('/orders/delOrder', payload);
    this.order = result;
  }
}

export default HomeStore;
